package com.recorder;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.recorder.recorder_lib.RecordingManager;
import com.recorder.recorder_lib.core.OnRecordingListener;
import com.recorder.recorder_lib.core.RecordingService;
import com.recorder.recorder_lib.utils.FileManager;

public class MainActivity extends AppCompatActivity {
    RecordingManager mRecordingManager;
    Button btnStart;
    Button btnEnd;
    Button btnRefresh;
    private RecordingService mRecordingService;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mRecordingService = ((RecordingService.RecordingBinder) service).getService();
            //回调监听
            mRecordingService.setOnRecordingListener(new OnRecordingListener() {

                @Override
                public void onStart() {
                    Toast.makeText(MainActivity.this, "服务开始了", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onProgress(long duringTime) {

                }

                @Override
                public void onEnd() {
                    Toast.makeText(MainActivity.this, "服务结束了", Toast.LENGTH_SHORT).show();
                }
            });
        }

        public void onServiceDisconnected(ComponentName className) {
            mRecordingService = null;
            Toast.makeText(MainActivity.this, "服务关闭了", Toast.LENGTH_SHORT).show();
        }
    };
    private TextView tvTime;
    private ListView lvDir;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        btnStart = findViewById(R.id.btn_start);
        btnEnd = findViewById(R.id.btn_end);
        btnRefresh = findViewById(R.id.btn_refresh);
        tvTime = findViewById(R.id.tv_time);
        lvDir = findViewById(R.id.lv_dir);
        init();
        initListener();
    }

    private void initListener() {
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });
        btnEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                end();
            }
        });
        btnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lvDir.setAdapter(new DirAdapter(FileManager.getFiles(FileManager.getDirPath())));
            }
        });
    }

    private void init() {
        mRecordingManager = new RecordingManager(Config.dirName, mConnection);
    }

    private void start() {
        mRecordingManager.startRecord(this);
    }

    public void end() {
        mRecordingManager.endRecord(this);
    }
}
